package com.flowable.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.util.DateUtils;
import com.flowable.domain.dto.SysForm;
import com.flowable.mapper.SysFormMapper;
import com.flowable.service.ISysFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 流程表单Service业务层处理
 *
 * @author Tony
 * @date 2021-04-03
 */
@Service
public class SysFormServiceImpl implements ISysFormService {
    @Autowired
    private SysFormMapper sysFormMapper;

    /**
     * 查询流程表单
     *
     * @param formId 流程表单ID
     * @return 流程表单
     */
    @Override
    public SysForm selectSysFormById(Long formId) {
        return sysFormMapper.selectSysFormById(formId);
    }

    /**
     * 查询流程表单列表
     *
     * @param sysForm 流程表单
     * @return 流程表单
     */
    @Override
    public ResponseEntity selectSysFormList(Page pageDto, SysForm sysForm) {
        IPage<SysForm> data = sysFormMapper.selectSysFormList(new Page<>(pageDto.getCurrent(), pageDto.getSize()), sysForm);
        return ResponseEntity.ok(data);
    }

    /**
     * 新增流程表单
     *
     * @param sysForm 流程表单
     * @return 结果
     */
    @Override
    public int insertSysForm(SysForm sysForm) {
        sysForm.setCreateTime(DateUtils.getNowDate());
        return sysFormMapper.insertSysForm(sysForm);
    }

    /**
     * 更新流程表单
     *
     * @param sysForm
     */
    @Override
    public ResponseEntity save(SysForm sysForm) {
        if (ObjectUtils.isEmpty(sysForm.getFormId())) {
            return ResponseEntity.ok(insertSysForm(sysForm));
        } else {
            return ResponseEntity.ok(updateSysForm(sysForm));
        }

    }

    /**
     * 修改流程表单
     *
     * @param sysForm 流程表单
     * @return 结果
     */
    @Override
    public int updateSysForm(SysForm sysForm) {
        sysForm.setUpdateTime(DateUtils.getNowDate());
        return sysFormMapper.updateSysForm(sysForm);
    }

    /**
     * 批量删除流程表单
     *
     * @param formIds 需要删除的流程表单ID
     * @return 结果
     */
    @Override
    public ResponseEntity deleteSysFormByIds(Long[] formIds) {
        return ResponseEntity.ok(sysFormMapper.deleteSysFormByIds(formIds));
    }

    /**
     * 删除流程表单信息
     *
     * @param formId 流程表单ID
     * @return 结果
     */
    @Override
    public int deleteSysFormById(Long formId) {
        return sysFormMapper.deleteSysFormById(formId);
    }

    @Override
    public ResponseEntity getLableByIds(List<String> ids) {
        List<SysForm> sysForms = sysFormMapper.selectList(new LambdaQueryWrapper<SysForm>()
                .select(SysForm::getFormId, SysForm::getFormName)
                .in(SysForm::getFormId,ids));
        return ResponseEntity.ok(sysForms);
    }
}
